<template>
	<view>

		<uni-forms style="width: 500rpx; margin: 50rpx auto 0rpx;" ref="passwordForm" :rules="passwordRules">
			<uni-forms-item>
				<uni-easyinput placeholder="请输入旧密码" type="password" v-model="passwordData.oldPassword" />
			</uni-forms-item>
			<uni-forms-item>
				<uni-easyinput placeholder="请输入新密码" type="password" v-model="passwordData.newPassword" />
			</uni-forms-item>
			<uni-forms-item>
				<uni-easyinput placeholder="确认密码" type="password" v-model="passwordData.confirmPassword" />
			</uni-forms-item>
		</uni-forms>

		<view style="width: 150rpx; margin: 0 auto;">
			<u-button @click="submit">提交</u-button>
		</view>
	</view>
</template>

<script>
	import {
		changeUserInfo
	} from '@/api/user/index.js'

	export default {
		data() {
			return {
				passwordData: {
					oldPassword: '',
					newPassword: '',
					confirmPassword: '',
				},
				passwordRules: {
					oldPassword: {
						rules: [{
							required: true,
							errorMessage: '请输入旧密码',
						}]
					},
					newPassword: {
						rules: [{
							required: true,
							errorMessage: '请输入新密码',
						}]
					},
					confirmPassword: {
						rules: [{
							required: true,
							errorMessage: '请确认密码',
						}, {
							validateFunction: function(rule, value, data, callback) {
								if (value != this.logonData.newPassword) {
									callback('二次密码啊不一致')
								}
								return true
							}
						}]
					}
				}
			}
		},
		onReady() {
			// this.$refs.passwordForm.setRules(this.password)
			uni.setNavigationBarColor({
				frontColor: '#000000',
				backgroundColor: uni.getStorageSync('backgroundColor')
			})
		},
		methods: {
			submit() {
				if (this.passwordData.newPassword != this.passwordData.confirmPassword) {
					uni.showToast({
						title: '两次密码不一致',
						icon: 'error'
					});
					return
				}
				
				changeUserInfo({
					oldPassword: this.passwordData.oldPassword,
					newPassword: this.passwordData.newPassword
				}).then((res) => {
					if (res == null) {
						return
					}
					uni.showToast({
						title: '修改成功',
						icon: 'success'
					});
				})
			}
		}
	}
</script>

<style>

</style>
